<?php

namespace App\Models;

use App\Services\ProjectService;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use HasDateTimeFormatter;
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    public $timestamps = true;

    protected $table = 'eb_project';

    // 项目关联会议 一对多
    public function meeting()
    {
        return $this->hasMany(ProjectMeeting::class, 'project_id', 'id');
    }
    // 一对多的讲者
    public function invitationSpeaker()
    {
        return $this->hasMany(ProjectMeetingInvitationSpeaker::class, 'project_id', 'id');
    }

    public function getOrderAmountAttribute($value)
    {
        return !empty($value) && isset($value) ? number_format($value, 2, '.', ',') : '';
    }

    /**
     * 只查没有被删除的作用域
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeDeleteTime($query)
    {
        return $query->whereNull('delete_time');
    }



    /**
     * 获取项目列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-10-09
     * @param [type] $payload
     * @return void
     */
    public function getProjectList($payload, $userId)
    {
        $where = function ($query) use ($payload, $userId) {
            $query->where('user_id', $userId);
            $query->where('is_display', 1);

            if (isset($payload['project_name']) && $payload['project_name'] != '') {
                $query->where('project_name', 'like', '%' . $payload['project_name'] . '%');
            }

            if (isset($payload['service']) && $payload['service'] != '') {
                $query->where('service', 'like', '%' . $payload['service'] . '%');
            }

            if (isset($payload['project_status']) && $payload['project_status'] != '') {
                $query->where('project_status',  $payload['project_status']);
            }

            if (isset($payload['create_time']) && $payload['create_time'] != '') {
                $query->whereDate('create_time',  $payload['create_time']);
            }
        };

        $list = $this->deleteTime()->where($where)
            ->select(
                'id',
                'project_name',
                'service',
                'user_id',
                'meeting_type',
                'company_name',
                'student_association',
                'foundation',
                'third_party',
                'order_amount',
                'project_status',
                'create_time',
                'stop_remarks',
                'stop_time'
            )
            ->orderBy('create_time', 'desc')
            ->paginate($payload['limit']);
        foreach ($list as $key => $val) {
            if ($val['project_status'] == 3) {
                $val['project_progress'] = 0;
            } elseif ($val['project_status'] == 2) {
                $val['project_progress'] = 4;
            } else {
                $val['project_progress'] = ProjectService::getProjectProgress($val['id']);
            }
        }
        return $list;
    }
}
